<?php

/**
 * @copyright Copyright(c) 2016
 * @brief 后台系统菜单管理
 * @author jumark
 * @date 2016/7/21
 * @version 1.0
 */

namespace Operation\Model;

use Think\Plugin;
use Think\ICookie;
use Think\IUrl;

class Menu {

    //菜单的配制数据
    public static $menu = array(
        '商品' => array(
            '二维码管理' => array(
                'Code/code_list' => '二维码列表',
                'Code/code_edit' => '生成二维码',
                'Code/code_recycle_list' => '二维码回收站',
            ),
            '商品管理' => array(
                'Goods/goods_list' => '商品列表',
                'Goods/goods_edit' => '商品添加',
                'Goods/goods_recycle_list' => '商品回收站'
            ),
            '商品分类' => array(
                'Goods/category_list' => '分类列表',
                'Goods/category_edit' => '添加分类'
            ),
            '品牌' => array(
                'Goods/brand_category' => '品牌分类',
                'Goods/brand_list' => '品牌列表'
            ),
            '模型' => array(
                'Goods/model_list' => '模型列表',
                'Goods/spec_list' => '规格列表',
                'Goods/spec_photo' => '规格图库'
            ),
        ),
        '商户' => array(
            '商户管理' => array(
                'Partner/seller_list' => '商户列表',
                'Partner/seller_edit' => '添加商户'
            ),
            '文章管理' => array(
                'Article/article_cat_list' => '文章分类',
                'Article/article_list' => '文章列表'
            ),
        ),
        '促销' => array(
            '优惠券' => array(
                'Operation/Game/coupon_list/' => '优惠券列表',
                'Operation/Game/coupon_edit/' => '新建优惠券'
            ),
            '营销活动' => array(
                'Operation/Game/game_list/type/1' => '幸运大转盘',
                'Operation/Game/game_list/type/2' => '好运翻翻看',
                'Operation/Game/game_list/type/3' => '微信红包'
            ),
        ),
        '统计' => array(
            '基础数据统计' => array(
                'Chart/index' => '基础数据统计',
                'market/spanding_avg' => '人均消费统计',
                'market/amount' => '销售金额统计'
            ),
            '日志操作记录' => array(
                'market/account_list' => '充值操作记录',
                'market/operation_list' => '后台操作记录',
            ),
            '商户数据统计' => array(
                'market/order_goods_list' => '货款明细列表',
                'market/bill_list' => '货款结算申请',
            )
        ),
        '系统' => array(
            '后台首页' => array(
                'Index/index' => '后台首页',
            ),
            '地域管理' => array(
                'System/area_list' => '地区列表',
            ),
            '权限管理' => array(
                'System/admin_list' => '管理员',
                'System/role_list' => '角色',
                'System/right_list' => '权限资源'
            ),
        ),
        '工具' => array(
            '数据库管理' => array(
                'Tool/db_bak' => '数据库备份',
                'Tool/db_res' => '数据库还原',
            ),
            '网站地图' => array(
                'tools/seo_sitemaps' => '网站搜索地图',
            )
        )
    );
    //非菜单连接映射关系,array(视图名称 => menu数组中已存在的菜单连接)
    public static $innerPathUrl = array(
        "system/navigation" => "Index/index",
        "system/navigation_edit" => "Index/index",
    );

    /**
     * @brief 根据权限初始化菜单
     * @param int $roleId 角色ID
     * @return array 菜单数组
     */
    public static function init($roleId) {
        //菜单创建事件触发
        Plugin::trigger("onSystemMenuCreate");

        //根据角色分配权限
        if ($roleId == 0) {
            $adminRights = 'administrator';
        } else {
            $roleObj = M('admin_role');
            $where = 'id = ' . $roleId . ' and is_del = 0';
            $roleRow = $roleObj->where($where)->find();
            $adminRights = isset($roleRow['rights']) ? $roleRow['rights'] : '';
        }

        //1,超管返回全部菜单
        if ($adminRights == "administrator") {
            return self::$menu;
        }

        //2,根据权限码显示菜单
        $result = array();
        $defaultShow = array('Index/index');
        foreach (self::$menu as $key1 => $val1) {
            foreach ($val1 as $key2 => $val2) {
                foreach ($val2 as $key3 => $val3) {
                    //把菜单数据里面的路径转化成@符号做权限码比对
                    $tempKey3 = str_replace("/", "@", trim($key3, "/"));
                    if (in_array($key3, $defaultShow) || strpos($adminRights, $tempKey3) !== false) {
                        $result[$key1][$key2][$key3] = $val3;
                    }
                }
            }
        }
        return $result;
    }

    /**
     * @brief 根据当前URL动态生成菜单分组
     * @param array  $menu 菜单数据
     * @param string $info 连接信息
     * @return array 菜单数组
     */
    public static function get($menu, $info) {
        //已有菜单查找
        foreach ($menu as $key1 => $val1) {
            foreach ($val1 as $key2 => $val2) {
                foreach ($val2 as $key3 => $val3) {
                    if (strpos($key3, $info) !== false || strpos($info, $key3) !== false) {
                        return array($key1 => $menu[$key1]);
                    }
                }
            }
        }

        //配置菜单映射
        if (self::$innerPathUrl) {
            foreach (self::$innerPathUrl as $key => $val) {
                if (strpos($key, $info) !== false) {
                    return self::get($menu, $val);
                }
            }
        }

        //历史URL信息
        $lastInfo = IUrl::getRefRoute();
        if (stripos($lastInfo, "/?") === 0) {
            $lastInfo = IUrl::convertUrl($lastInfo, IUrl::UrlNative, IUrl::UrlPathinfo);
        }

        if ($lastInfo && strpos($lastInfo, $info) === false) {
            ICookie::set('lastInfo', $lastInfo);
            return self::get($menu, $lastInfo);
        }

        //刷新页面
        $lastInfo = ICookie::get('lastInfo');
        if ($lastInfo) {
            return FALSE;
            //return self::get($menu, $lastInfo);
        }
        return array($key1 => $menu[$key1]);
    }

    /**
     * @brief 获取顶级分类关系数据
     * @param array $menu 菜单数据
     * @return array 顶级菜单数组
     */
    public static function getTopMenu($menu) {
        $result = array();
        foreach ($menu as $key1 => $val1) {
            foreach ($val1 as $key2 => $val2) {
                foreach ($val2 as $key3 => $val3) {
                    $result[$key1] = $key3;
                    break 2;
                }
            }
        }
        return $result;
    }

    public function __destruct() {
        
    }

}